package cn.lg.soar.system.biz.modules.auth.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.AuthRole;
import cn.lg.soar.system.api.model.RoleDTO;
import cn.lg.soar.system.api.service.IRoleApi;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.auth.entity.Role;
import cn.lg.soar.system.biz.modules.auth.service.*;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/role")
public class RoleApiClient implements IRoleApi {

    @Autowired
    private IRoleService service;
    @Autowired
    private UserRoleRelService userRoleRelService;
    @Autowired
    private PostRoleRelService postRoleRelService;
    @Autowired
    private IUserService userService;

    @GetMapping("idsByUserId")
    @Override
    public List<Long> idsByUserId(@RequestParam(name = "userId") Long userId) {
        return userRoleRelService.getRoleIds(userId);
    }

    @GetMapping("idsByPostId")
    @Override
    public List<Long> idsByPostId(@RequestParam(name = "postId") Long postId) {
        return postRoleRelService.getRoleIds(postId);
    }

    @GetMapping("listByCodes")
    @Override
    public List<RoleDTO> listByCodes(@RequestParam(name = "codes") Collection<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return service.listByCodes(codes).stream().map(this::toDto).collect(Collectors.toList());
    }

    @GetMapping("listByIds")
    @Override
    public List<RoleDTO> listByIds(@RequestParam(name = "ids") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return service.listByIds(ids).stream().map(this::toDto).collect(Collectors.toList());
    }

    @GetMapping("getById")
    @Override
    public RoleDTO getById(@RequestParam(name = "id") Long id) {
        return toDto(service.getById(id));
    }

    @GetMapping("codesByIds")
    @Override
    public List<String> codesByIds(@RequestParam(name = "roleIds") List<Long> roleIds) {
        return service.codesByIds(roleIds);
    }

    @GetMapping("userRoleList")
    @Override
    public List<AuthRole> userRoleList(Long userId) {
        List<Long> roleIds = userService.getUserCurrentAllRoleIds(userId);
        List<Role> roles = service.listByIds(roleIds, SystemCacheKey.ROLEID_TO_ROLE);
        return roles.stream().map(x -> {
            AuthRole authRole = new AuthRole();
            authRole.setId(x.getId());
            authRole.setCode(x.getCode());
            authRole.setName(x.getName());
            authRole.setAlias(x.getAlias());
            authRole.setRemark(x.getRemark());
            return authRole;
        }).collect(Collectors.toList());
    }

    private RoleDTO toDto(Role role) {
        RoleDTO dto = new RoleDTO();
        dto.setId(role.getId());
        dto.setCode(role.getCode());
        dto.setName(role.getName());
        dto.setRemark(role.getRemark());
        return dto;
    }
}
